{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "import numpy as np\n",
    "import cv2\n",
    "from mtcnn.mtcnn import mtcnn\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "m_mtcnn = mtcnn()\n",
    "rgb_boxes, rgb_points = m_mtcnn.detect_face('timg.jpg')\n",
    "gray_boxes, _ = m_mtcnn.detect_face('timg1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgb_boxes.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gray_boxes.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(image_rgb, image_gray):\n",
    "    rgb_boxes, _ = m_mtcnn.detect_face(image_rgb)\n",
    "    gray_boxes, _ = m_mtcnn.detect_face(image_gray)\n",
    "    if type(rgb_boxes) is np.ndarray and type(gray_boxes) is np.ndarray:\n",
    "        rgb_faces = rgb_boxes.shape[0]\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 343.21069336,  110.53774261,  486.16165161,  313.04003906,\n",
       "           0.99996388],\n",
       "       [ 796.8682251 ,  209.19934082,  985.94659424,  438.26367188,\n",
       "           0.99986923],\n",
       "       [  82.67982483,  382.41781616,  280.68753052,  626.45721436,\n",
       "           0.99714631]], dtype=float32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gray_boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 344.14044189,  111.1885376 ,  484.92965698,  312.29153442,\n",
       "           0.99992728],\n",
       "       [ 793.96234131,  208.69070435,  985.77648926,  437.98638916,\n",
       "           0.99989736],\n",
       "       [ 621.13757324,  111.50782013,  774.93127441,  328.90014648,\n",
       "           0.99933511],\n",
       "       [  82.60527802,  382.84649658,  280.21679688,  627.03839111,\n",
       "           0.99641389]], dtype=float32)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgb_boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "rgb_x1 = rgb_boxes[:,0]\n",
    "rgb_y1 = rgb_boxes[:,1]\n",
    "rgb_x2 = rgb_boxes[:,2]\n",
    "rgb_y2 = rgb_boxes[:,3]\n",
    "rgb_area = (rgb_x2-rgb_x1+1) * (rgb_y2-rgb_y1+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "gray_x1 = gray_boxes[:,0]\n",
    "gray_y1 = gray_boxes[:,1]\n",
    "gray_x2 = gray_boxes[:,2]\n",
    "gray_y2 = gray_boxes[:,3]\n",
    "gray_area = (gray_x2-gray_x1+1) * (gray_y2-gray_y1+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "nms_thread = 0.8\n",
    "rgb_retain_indexes = []\n",
    "rgb_rm_indexes = []\n",
    "for rgb_index in range(0, rgb_boxes.shape[0]):\n",
    "    for gray_index in range(0, gray_boxes.shape[0]):\n",
    "        overlap_x1 = np.maximum(rgb_x1[rgb_index], gray_x1[gray_index])\n",
    "        overlap_y1 = np.maximum(rgb_y1[rgb_index], gray_y1[gray_index])\n",
    "        overlap_x2 = np.minimum(rgb_x2[rgb_index], gray_x2[gray_index])\n",
    "        overlap_y2 = np.minimum(rgb_y2[rgb_index], gray_y2[gray_index])\n",
    "        overlap_w = np.maximum(0.0, overlap_x2 - overlap_x1 + 1)\n",
    "        overlap_h = np.maximum(0.0, overlap_y2 - overlap_y1 + 1)\n",
    "        overlap_area = overlap_w * overlap_h\n",
    "        overlap_ratio = overlap_area / (rgb_area[rgb_index] + gray_area[gray_index] - overlap_area)\n",
    "        if overlap_ratio > nms_thread:\n",
    "            rgb_retain_indexes.append(rgb_index)\n",
    "    if not rgb_index in rgb_retain_indexes:\n",
    "        rgb_rm_indexes.append(rgb_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_a = np.delete(rgb_boxes, rgb_rm_indexes, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_p = np.delete(rgb_points, rgb_rm_indexes, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
